import { _decorator, Collider2D, Vec2, Vec3 } from 'cc';
import { Unit } from './Unit';
import { Bullet } from './Bullet';
const { ccclass, property } = _decorator;

@ccclass('Bullet_2')
export class Bullet_2 extends Bullet {

    updateLogic(dt) {
        if (this._lifeTime >= this.canLifeTime) {

            this.DestroySelf();
        }

        this.rigBody.linearVelocity = this.moveVec.normalize().multiplyScalar(this.maxSpeed);

        if (this.moveVec.length() == 0) {
            this.DestroySelf();
        }

        let angle = this.rigBody.linearVelocity.signAngle(this.forward) * 180 / Math.PI;
        this.node.setRotationFromEuler(0, 0, -angle);
    }

    /**设置目标 只会第一次初始子弹移动方向 不会保存目标*/
    SetTarget(target: Unit) {
        // this.target = target;
        if (target) {
            target.node.getWorldPosition(this._targetWorldPos);
            let targetV = target.rigBody.linearVelocity.normalize();
            let dis = this.DistanceOther(target);
            let time = dis / this.maxSpeed;
            targetV.multiplyScalar(time * target.speed);
            this._targetWorldPos.x += targetV.x;
            this._targetWorldPos.y += targetV.y;
            let selfWorldPos = this.node.getWorldPosition();
            let moveDir = this._targetWorldPos.subtract(selfWorldPos).normalize();

            this.moveVec.set(moveDir.x, moveDir.y);
        }

        if (!this.IsEnemy(target)) {
            console.log('警告==>目标是自己人！！？');
        }
    }

    AutoMove() {
        super.AutoMove();
        let angle = this.rigBody.linearVelocity.signAngle(this.forward) * 180 / Math.PI;
        this.node.setRotationFromEuler(0, 0, -angle); //= Quat.fromEuler(this.node.rotation,0, 0, -angle);
    }
}


